<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>ES6系列之Reflect | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="ES6,Javascript,">
  

  <meta name="description" content="一、简介1.1 什么是Reflect 为操作对象而提供的新API  1.2 为什么要设计Reflect 将Object对象的属于语言内部的方法放到Reflect对象上，即从Reflect对象上拿Object对象内部方法 将用老Object方法报错的情况，改为返回false  // 旧写法try &amp;#123;  Object.defineProperty(target, property, attr">
<meta name="keywords" content="ES6,Javascript">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6系列之Reflect">
<meta property="og:url" content="http://blog.poetries.top/2018/12/21/es6-reflect/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、简介1.1 什么是Reflect 为操作对象而提供的新API  1.2 为什么要设计Reflect 将Object对象的属于语言内部的方法放到Reflect对象上，即从Reflect对象上拿Object对象内部方法 将用老Object方法报错的情况，改为返回false  // 旧写法try &amp;#123;  Object.defineProperty(target, property, attr">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.906Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ES6系列之Reflect">
<meta name="twitter:description" content="一、简介1.1 什么是Reflect 为操作对象而提供的新API  1.2 为什么要设计Reflect 将Object对象的属于语言内部的方法放到Reflect对象上，即从Reflect对象上拿Object对象内部方法 将用老Object方法报错的情况，改为返回false  // 旧写法try &amp;#123;  Object.defineProperty(target, property, attr">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、简介"><span class="toc-text">一、简介</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-什么是Reflect"><span class="toc-text">1.1 什么是Reflect</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-为什么要设计Reflect"><span class="toc-text">1.2 为什么要设计Reflect</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、Reflect的API"><span class="toc-text">二、Reflect的API</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-Reflect-get-target-property-receiver"><span class="toc-text">2.1 Reflect.get(target,property,receiver)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-Reflect-set-target-propName-propValue-receiver"><span class="toc-text">2.2 Reflect.set(target,propName,propValue,receiver)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-Reflect-set与Proxy-set"><span class="toc-text">2.3 Reflect.set与Proxy.set</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-Reflect-has-obj-name"><span class="toc-text">2.4 Reflect.has(obj,name)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-Reflect-deleteProperty-obj-name"><span class="toc-text">2.5 Reflect.deleteProperty(obj, name)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-6-Reflect-construct-target-args"><span class="toc-text">2.6 Reflect.construct(target, args)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-Reflect-getPrototypeOf-obj"><span class="toc-text">2.7 Reflect.getPrototypeOf(obj)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-8-Reflect-setPrototypeOf-obj-newProto"><span class="toc-text">2.8 Reflect.setPrototypeOf(obj, newProto)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-9-Reflect-apply-func-thisArg-args"><span class="toc-text">2.9 Reflect.apply(func, thisArg, args)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-10-Reflect-defineProperty-target-propertyKey-attributes"><span class="toc-text">2.10 Reflect.defineProperty(target, propertyKey, attributes)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-11-Reflect-getOwnPropertyDescriptor-target-propertyKey"><span class="toc-text">2.11 Reflect.getOwnPropertyDescriptor(target, propertyKey)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-12-Reflect-isExtensible-target"><span class="toc-text">2.12 Reflect.isExtensible (target)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-13-Reflect-preventExtensions-target"><span class="toc-text">2.13 Reflect.preventExtensions(target)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-14-Reflect-ownKeys-target"><span class="toc-text">2.14 Reflect.ownKeys (target)</span></a></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-es6-reflect" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">ES6系列之Reflect</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.12.21</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 4分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、简介"><a href="#一、简介" class="headerlink" title="一、简介"></a>一、简介</h2><h3 id="1-1-什么是Reflect"><a href="#1-1-什么是Reflect" class="headerlink" title="1.1 什么是Reflect"></a>1.1 什么是Reflect</h3><blockquote>
<p>为操作对象而提供的新<code>API</code></p>
</blockquote>
<h3 id="1-2-为什么要设计Reflect"><a href="#1-2-为什么要设计Reflect" class="headerlink" title="1.2 为什么要设计Reflect"></a>1.2 为什么要设计Reflect</h3><ol>
<li>将<code>Object</code>对象的属于语言内部的方法放到<code>Reflect</code>对象上，即从<code>Reflect</code>对象上拿<code>Object</code>对象内部方法</li>
<li>将用老<code>Object</code>方法报错的情况，改为返回<code>false</code></li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 旧写法</span></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="built_in">Object</span>.defineProperty(target, property, attributes);</span><br><span class="line">  <span class="comment">// success</span></span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  <span class="comment">// failure</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 新写法</span></span><br><span class="line"><span class="keyword">if</span> (<span class="built_in">Reflect</span>.defineProperty(target, property, attributes)) &#123;</span><br><span class="line">  <span class="comment">// success</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="comment">// failure</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>让<code>Object</code>操作变成函数行为</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 旧写法</span></span><br><span class="line"><span class="string">'name'</span> <span class="keyword">in</span> <span class="built_in">Object</span> <span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 新写法</span></span><br><span class="line"><span class="built_in">Reflect</span>.has(<span class="built_in">Object</span>,<span class="string">'name'</span>) <span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<ol start="4">
<li><code>Reflect</code>与<code>Proxy</code>是相辅相成的，在<code>Proxy</code>上有的方法，在<code>Reflect</code>就一定有</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> target=&#123;&#125;</span><br><span class="line"><span class="keyword">let</span> handler=&#123;</span><br><span class="line">  <span class="keyword">set</span>(target,proName,proValue,receiver)&#123;</span><br><span class="line">    <span class="comment">//确认对象的属性赋值成功</span></span><br><span class="line">    <span class="keyword">let</span> isSuccess=<span class="built_in">Reflect</span>.set(target,proName,proValue,receiver)</span><br><span class="line">    <span class="keyword">if</span>(isSuccess)&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">"成功"</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> isSuccess</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> proxy=<span class="keyword">new</span> <span class="built_in">Proxy</span>(target,handler)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>确保对象的属性能正确赋值，广义上讲，即确保对象的原生行为能够正常进行，这就是<code>Reflect</code>的作用</p>
</blockquote>
<h2 id="二、Reflect的API"><a href="#二、Reflect的API" class="headerlink" title="二、Reflect的API"></a>二、Reflect的API</h2><blockquote>
<p>注：和<code>Proxy</code>的<code>API</code>一致</p>
</blockquote>
<h3 id="2-1-Reflect-get-target-property-receiver"><a href="#2-1-Reflect-get-target-property-receiver" class="headerlink" title="2.1 Reflect.get(target,property,receiver)"></a>2.1 Reflect.get(target,property,receiver)</h3><blockquote>
<p>查找并返回<code>target</code>对象的<code>property</code>属性</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> obj=&#123;</span><br><span class="line">  name:<span class="string">"poetries"</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> result=<span class="built_in">Reflect</span>.get(obj,<span class="string">"name"</span>)</span><br><span class="line"><span class="built_in">console</span>.log(result) <span class="comment">//poetries</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> obj=&#123;</span><br><span class="line">  <span class="comment">//属性yu部署了getter读取函数</span></span><br><span class="line">  <span class="keyword">get</span> yu()&#123;</span><br><span class="line">    <span class="comment">//this返回的是Reflect.get的receiver参数对象</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.name+<span class="keyword">this</span>.age</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> receiver=&#123;</span><br><span class="line">  name:<span class="string">"shen"</span>,</span><br><span class="line">  age:<span class="string">"18"</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> result=<span class="built_in">Reflect</span>.get(obj,<span class="string">"yu"</span>,receiver)</span><br><span class="line"><span class="built_in">console</span>.log(result) <span class="comment">//shen18</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：如果<code>Reflect.get()</code>的第一个参数不是对象，则会报错</p>
</blockquote>
<h3 id="2-2-Reflect-set-target-propName-propValue-receiver"><a href="#2-2-Reflect-set-target-propName-propValue-receiver" class="headerlink" title="2.2 Reflect.set(target,propName,propValue,receiver)"></a>2.2 Reflect.set(target,propName,propValue,receiver)</h3><blockquote>
<p>设置<code>target</code>对象的<code>propName</code>属性为<code>propValue</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> obj=&#123;</span><br><span class="line">  name:<span class="string">"poetries"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> result=<span class="built_in">Reflect</span>.set(obj,<span class="string">"name"</span>,<span class="string">"静观流叶"</span>)</span><br><span class="line"><span class="built_in">console</span>.log(result) <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.name) <span class="comment">//静观流叶</span></span><br></pre></td></tr></table></figure>
<h3 id="2-3-Reflect-set与Proxy-set"><a href="#2-3-Reflect-set与Proxy-set" class="headerlink" title="2.3 Reflect.set与Proxy.set"></a>2.3 Reflect.set与Proxy.set</h3><blockquote>
<p><code>Reflect.set</code>与<code>Proxy.set</code>联合使用,并且传入<code>receiver</code>，则会进行定义属性操作</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> obj=&#123;</span><br><span class="line">  name:<span class="string">"chen"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> handler=&#123;</span><br><span class="line">  <span class="keyword">set</span>(target,key,value,receiver)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"Proxy拦截赋值操作"</span>)</span><br><span class="line">    <span class="comment">//Reflect完成赋值操作</span></span><br><span class="line">    <span class="built_in">Reflect</span>.set(target,key,value,receiver)</span><br><span class="line">  &#125;,</span><br><span class="line">  defineProperty(target,key,attribute)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"Proxy拦截定义属性操作"</span>)</span><br><span class="line">    <span class="comment">//Reflect完成定义属性操作</span></span><br><span class="line">    <span class="built_in">Reflect</span>.defineProperty(target,key,attribute)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> proxy=<span class="keyword">new</span> <span class="built_in">Proxy</span>(obj,handler)</span><br><span class="line">proxy.name=<span class="string">"ya"</span></span><br><span class="line"><span class="comment">//Proxy拦截赋值操作</span></span><br><span class="line"><span class="comment">//Proxy拦截定义属性操作</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>为什么Reflect.set()传入receiver参数，就会触发定义属性的操作？</p>
</blockquote>
<p>因为<code>Proxy.set()</code>中的<code>receiver</code>是<code>Proxy</code>的实例，即<code>obj</code>，而<code>Reflect.set</code>一旦传入<code>receiver</code>，就会将属性赋值到<code>receiver</code>上面，也是<code>obj</code>，所以就会触发<code>defineProperty</code>拦截</p>
<h3 id="2-4-Reflect-has-obj-name"><a href="#2-4-Reflect-has-obj-name" class="headerlink" title="2.4 Reflect.has(obj,name)"></a>2.4 Reflect.has(obj,name)</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> obj= &#123;</span><br><span class="line">  name: <span class="string">"poetries"</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//旧写法</span></span><br><span class="line"><span class="string">'name'</span> <span class="keyword">in</span> obj <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//新写法</span></span><br><span class="line"><span class="built_in">Reflect</span>.has(obj, <span class="string">'name'</span>) <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<h3 id="2-5-Reflect-deleteProperty-obj-name"><a href="#2-5-Reflect-deleteProperty-obj-name" class="headerlink" title="2.5 Reflect.deleteProperty(obj, name)"></a>2.5 Reflect.deleteProperty(obj, name)</h3><blockquote>
<p>删除对象的属性</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 旧写法</span></span><br><span class="line"><span class="keyword">delete</span> obj.name;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 新写法</span></span><br><span class="line"><span class="built_in">Reflect</span>.deleteProperty(obj, <span class="string">'name'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="2-6-Reflect-construct-target-args"><a href="#2-6-Reflect-construct-target-args" class="headerlink" title="2.6 Reflect.construct(target, args)"></a>2.6 Reflect.construct(target, args)</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.name = name;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 旧 new写法</span></span><br><span class="line"><span class="keyword">let</span> person= <span class="keyword">new</span> Person(<span class="string">'poetries'</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 新写法：Reflect.construct 的写法</span></span><br><span class="line"><span class="keyword">let</span> person = <span class="built_in">Reflect</span>.construct(Person, [<span class="string">'poetries'</span>]);</span><br></pre></td></tr></table></figure>
<h3 id="2-7-Reflect-getPrototypeOf-obj"><a href="#2-7-Reflect-getPrototypeOf-obj" class="headerlink" title="2.7 Reflect.getPrototypeOf(obj)"></a>2.7 Reflect.getPrototypeOf(obj)</h3><blockquote>
<p>用于读取对象的<code>proto</code>属性，对应<code>Object.getPrototypeOf(obj)</code></p>
</blockquote>
<h3 id="2-8-Reflect-setPrototypeOf-obj-newProto"><a href="#2-8-Reflect-setPrototypeOf-obj-newProto" class="headerlink" title="2.8 Reflect.setPrototypeOf(obj, newProto)"></a>2.8 Reflect.setPrototypeOf(obj, newProto)</h3><blockquote>
<p>设置目标对象的原型（<code>prototype</code>），对应<code>Object.setPrototypeOf(obj, newProto)</code>方法</p>
</blockquote>
<h3 id="2-9-Reflect-apply-func-thisArg-args"><a href="#2-9-Reflect-apply-func-thisArg-args" class="headerlink" title="2.9 Reflect.apply(func, thisArg, args)"></a>2.9 Reflect.apply(func, thisArg, args)</h3><blockquote>
<p>继承目标对象的特定方法</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> array=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 旧写法</span></span><br><span class="line"><span class="keyword">let</span> small= <span class="built_in">Math</span>.min.apply(<span class="built_in">Math</span>, array) <span class="comment">//1</span></span><br><span class="line"><span class="keyword">let</span> big = <span class="built_in">Math</span>.max.apply(<span class="built_in">Math</span>, array) <span class="comment">//6</span></span><br><span class="line"><span class="keyword">let</span> type = <span class="built_in">Object</span>.prototype.toString.call(small) <span class="comment">//"[object Number]"</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 新写法</span></span><br><span class="line"><span class="keyword">const</span> small= <span class="built_in">Reflect</span>.apply(<span class="built_in">Math</span>.min, <span class="built_in">Math</span>, array)</span><br><span class="line"><span class="keyword">const</span> big = <span class="built_in">Reflect</span>.apply(<span class="built_in">Math</span>.max, <span class="built_in">Math</span>, array)</span><br><span class="line"><span class="comment">//第三个参数是Object类型的就好，因为调用的是Object的原型方法toString</span></span><br><span class="line"><span class="keyword">const</span> type = <span class="built_in">Reflect</span>.apply(<span class="built_in">Object</span>.prototype.toString, small, [])</span><br></pre></td></tr></table></figure>
<h3 id="2-10-Reflect-defineProperty-target-propertyKey-attributes"><a href="#2-10-Reflect-defineProperty-target-propertyKey-attributes" class="headerlink" title="2.10 Reflect.defineProperty(target, propertyKey, attributes)"></a>2.10 Reflect.defineProperty(target, propertyKey, attributes)</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">MyDate</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  ...</span><br><span class="line">  ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 旧写法</span></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(MyDate, <span class="string">'now'</span>, &#123;</span><br><span class="line">  value: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">Date</span>.now()</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 新写法</span></span><br><span class="line"><span class="built_in">Reflect</span>.defineProperty(MyDate, <span class="string">'now'</span>, &#123;</span><br><span class="line">  value: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">Date</span>.now()</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>与<code>Proxy.defineProperty</code>配合使用</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> proxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(&#123;&#125;, &#123;</span><br><span class="line">  defineProperty(target, prop, descriptor) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(descriptor);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Reflect</span>.defineProperty(target, prop, descriptor);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">proxy .name= <span class="string">'chen'</span>;</span><br><span class="line"><span class="comment">// &#123;value: "chen", writable: true, enumerable: true, configurable: true&#125;</span></span><br><span class="line">p.name <span class="comment">// "chen"</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>如上，<code>Proxy.defineProperty</code>对属性赋值设置拦截，然后使用<code>Reflect.defineProperty</code>完成赋值</p>
</blockquote>
<h3 id="2-11-Reflect-getOwnPropertyDescriptor-target-propertyKey"><a href="#2-11-Reflect-getOwnPropertyDescriptor-target-propertyKey" class="headerlink" title="2.11 Reflect.getOwnPropertyDescriptor(target, propertyKey)"></a>2.11 Reflect.getOwnPropertyDescriptor(target, propertyKey)</h3><blockquote>
<p>基本等同于<code>Object.getOwnPropertyDescriptor</code>，用于得到指定属性的描述对象</p>
</blockquote>
<h3 id="2-12-Reflect-isExtensible-target"><a href="#2-12-Reflect-isExtensible-target" class="headerlink" title="2.12 Reflect.isExtensible (target)"></a>2.12 Reflect.isExtensible (target)</h3><blockquote>
<p>对应<code>Object.isExtensible</code>，返回一个布尔值，表示当前对象是否可扩展</p>
</blockquote>
<h3 id="2-13-Reflect-preventExtensions-target"><a href="#2-13-Reflect-preventExtensions-target" class="headerlink" title="2.13 Reflect.preventExtensions(target)"></a>2.13 Reflect.preventExtensions(target)</h3><blockquote>
<p>对应<code>Object.preventExtensions</code>方法，用于让一个对象变为不可扩展。它返回一个布尔值，表示是否操作成功</p>
</blockquote>
<h3 id="2-14-Reflect-ownKeys-target"><a href="#2-14-Reflect-ownKeys-target" class="headerlink" title="2.14 Reflect.ownKeys (target)"></a>2.14 Reflect.ownKeys (target)</h3><blockquote>
<p>用于返回对象的所有属性</p>
</blockquote>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/12/21/es6-proxy/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/12/21/es6-decorator/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
